/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPanel_XemMail.java
 *
 * Created on May 27, 2009, 9:23:59 AM
 */

package doanlythuyet_javaoutlook.MyUserControl;

import BUS.MyAttachBUS;
import BUS.MyMailBUS;
import DTO.MyMailDTO;
import doanlythuyet_javaoutlook.DoAnLyThuyet_JavaOutLookApp;
import doanlythuyet_javaoutlook.MyEmum.JEnum_TinhTrang;
import doanlythuyet_javaoutlook.MyMail;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.swing.AbstractButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.event.MouseInputListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;


/**
 *
 * @author Dang Thi Phuong Thao
 */
public class JPanel_XemMail extends javax.swing.JPanel {

    //private Boolean tatCaDuocChon = false;
    private final JPopupMenu popupMenu;
    /**
     * lay cac id mail duoc danh dau check trong bang
     * @return
     */
    public ArrayList<Integer> LayCacIdMailDuocChon (){
        ArrayList<Integer> Kq = new ArrayList<Integer>();
        for (int i = 0; i < jTable_danhsachmail.getRowCount(); i++)
            if (jTable_danhsachmail.getValueAt(i, 0) != null && (Boolean)jTable_danhsachmail.getValueAt(i, 0))
                Kq.add(Integer.parseInt(jTable_danhsachmail.getValueAt(i, jTable_danhsachmail.getColumnCount() - 1).toString()));
        return Kq;
    }
    private void ChuyenMailDTOLenGiaoDien (MyMailDTO mailDTO){
        FileInputStream inputStream = null;
        try {
            jLabel_chude.setText(mailDTO.getM_ChuDe());
            jLabel_goitu.setText(mailDTO.getM_NguoiGoi());
            jLabel_den.setText(mailDTO.getM_NguoiNhan());
            jLabel_ngay.setText(mailDTO.getM_Ngay().toString());
            inputStream = new FileInputStream(new File(DoAnLyThuyet_JavaOutLookApp.getCurdir() + mailDTO.getM_DuongDanFileChuaNoiDung()));
            MyMail mail = new MyMail(DoAnLyThuyet_JavaOutLookApp.getSmtp_host(), DoAnLyThuyet_JavaOutLookApp.getPop3_host(), DoAnLyThuyet_JavaOutLookApp.getUser()
                , DoAnLyThuyet_JavaOutLookApp.getPass(), DoAnLyThuyet_JavaOutLookApp.getProxy(), DoAnLyThuyet_JavaOutLookApp.getPort());
            Session sess = mail.getMailSession(false);
            MimeMessage mess = new MimeMessage(sess, inputStream);
            Part textPart = MyMail.layPartNoiDung(mess);

            if (textPart != null){
                jEditorPane_chitietmail.setContentType(textPart.getContentType());
                jEditorPane_chitietmail.setText(textPart.getContent().toString());
            }
            else{
                jEditorPane_chitietmail.setText(mess.getContent().toString());
            }

        } catch (Exception ex) {
            Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.toString());
        }
        try {
            inputStream.close();
        } catch (IOException ex) {
            Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void XemSoLuocMail (int MailID){
        try {
            jEditorPane_chitietmail.setText("viet ham lay Mine Message cua mailid: " + MailID + " (file JPanel_XamMail.java dong 55)");
            MyMailBUS mailBUS = new MyMailBUS();
            MyMailDTO mailDTO = mailBUS.layMailVoidMailID(String.valueOf(MailID));
            if (mailDTO.getM_TinhTrang() == JEnum_TinhTrang.Moi.value()){
                mailDTO.setM_TinhTrang(JEnum_TinhTrang.DaXem.value());
                mailBUS.capNhatMail(mailDTO);
                refreshBang(mailBUS);
            }
            if (mailDTO != null)
                ChuyenMailDTOLenGiaoDien(mailDTO);
            else return;
        } catch (IOException ex) {
            Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /** Creates new form JPanel_XemMail */
    public JPanel_XemMail() {
        initComponents();
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        jClass_ChuDe cacChuDe = new jClass_ChuDe();
        for (String str : jClass_ChuDe.getCacChuDe()){
            model.addElement(str);
        }
        //model.addElement("Thêm mới!");
        jComboBox1.setModel(model);
        popupMenu = new JPopupMenu();
        String[] cacHanhVi = {"Di Chuyen", "Copy", "Xoa"};

        for (String HanhVi : cacHanhVi)
        {
            JMenuItem menuItem = new JMenuItem(HanhVi);
            menuItem.addMouseListener(new PopupMenu_MouseListerner_MyBangDanhSachFile(menuItem.getText()));
            popupMenu.add(menuItem);
        }
        jTable_danhsachmail.addMouseListener(new MouseInputListener() {

            public void mouseClicked(MouseEvent e) {
                if (e.isPopupTrigger()){
                    //popupMenu.getComponentAt(e.getX(), e.getY()).get
                    //return;
                }
                if (jTable_danhsachmail.getSelectedRow() == -1)
                    return;
                //if (e.getButton() == MouseEvent.BUTTON1){

                int row = jTable_danhsachmail.getSelectedRow();
                //(PopupMenu_MouseListerner_MyBangDanhSachFile) popupMenu.getli
                //jTable_danhsachmail.getColumnModel().getColumn(1).getCellEditor().getCellEditorValue()
                //int MailID = Integer.getInteger(jTable_danhsachmail.getValueAt(row, jTable_danhsachmail.getColumnCount() - 1).toString());
                String strMailID = jTable_danhsachmail.getValueAt(row, jTable_danhsachmail.getColumnCount() - 1).toString();
                getPopupMenu().setToolTipText(strMailID);
                //int MailID = Integer.valueOf(strMailID);
                int MailID = Integer.valueOf(strMailID);
                //if (e.getClickCount() == 1){
                    //JOptionPane.showMessageDialog(null, MailID);
                    //jLabel_chude.setText(String.valueOf(MailID));
                    //jLabel_chude.setText(String.valueOf(MailID));
                    XemSoLuocMail(MailID);
                //}
                if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1){
                    JFrame_ChiTietMail chiTietMail = new JFrame_ChiTietMail(MailID);
                    chiTietMail.setVisible(true);
                }

                //}

                if (e.getButton() == MouseEvent.BUTTON3) {
                    getPopupMenu().show(e.getComponent(), e.getX(), e.getY());
                    //return;
                }
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mousePressed(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");

            }

            public void mouseReleased(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseEntered(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseDragged(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseMoved(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        //DuaDanhSachMailVaoBang(DanhSachMail);
    }

    public void ThemDongVaoBang (JTable table, Object[][] CacGiaTri){
        DefaultTableModel model = (DefaultTableModel) table.getModel();

        //model
    }
    public void XoaDongTrongBang (JTable table, int[] Indexs){
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        //model.
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel_DanhSachMail = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jToolBar1 = new javax.swing.JToolBar();
        jButton_Xoa = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton_DDDaDoc = new javax.swing.JButton();
        jButtonDDChuaDoc = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jButton_DiChuyen = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        jLabel5 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jPanel_ChiTietMail = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane_chitietmail = new javax.swing.JEditorPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel_chude = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel_goitu = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel_ngay = new javax.swing.JLabel();
        jLabel_den = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setName("Form"); // NOI18N
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jSplitPane1.setDividerLocation(200);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doanlythuyet_javaoutlook.DoAnLyThuyet_JavaOutLookApp.class).getContext().getResourceMap(JPanel_XemMail.class);
        jPanel_DanhSachMail.setForeground(resourceMap.getColor("jPanel_DanhSachMail.foreground")); // NOI18N
        jPanel_DanhSachMail.setFont(resourceMap.getFont("jPanel_DanhSachMail.font")); // NOI18N
        jPanel_DanhSachMail.setName("jPanel_DanhSachMail"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        jButton_Xoa.setIcon(resourceMap.getIcon("jButton_Xoa.icon")); // NOI18N
        jButton_Xoa.setText(resourceMap.getString("jButton_Xoa.text")); // NOI18N
        jButton_Xoa.setFocusable(false);
        jButton_Xoa.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton_Xoa.setName("jButton_Xoa"); // NOI18N
        jButton_Xoa.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton_Xoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_XoaActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton_Xoa);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar1.add(jSeparator1);

        jButton_DDDaDoc.setIcon(resourceMap.getIcon("jButton_DDDaDoc.icon")); // NOI18N
        jButton_DDDaDoc.setText(resourceMap.getString("jButton_DDDaDoc.text")); // NOI18N
        jButton_DDDaDoc.setFocusable(false);
        jButton_DDDaDoc.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton_DDDaDoc.setName("jButton_DDDaDoc"); // NOI18N
        jButton_DDDaDoc.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton_DDDaDoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_DDDaDocActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton_DDDaDoc);

        jButtonDDChuaDoc.setIcon(resourceMap.getIcon("jButtonDDChuaDoc.icon")); // NOI18N
        jButtonDDChuaDoc.setText(resourceMap.getString("jButtonDDChuaDoc.text")); // NOI18N
        jButtonDDChuaDoc.setFocusable(false);
        jButtonDDChuaDoc.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonDDChuaDoc.setName("jButtonDDChuaDoc"); // NOI18N
        jButtonDDChuaDoc.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonDDChuaDoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDDChuaDocActionPerformed(evt);
            }
        });
        jToolBar1.add(jButtonDDChuaDoc);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        jButton_DiChuyen.setIcon(resourceMap.getIcon("jButton_DiChuyen.icon")); // NOI18N
        jButton_DiChuyen.setText(resourceMap.getString("jButton_DiChuyen.text")); // NOI18N
        jButton_DiChuyen.setFocusable(false);
        jButton_DiChuyen.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton_DiChuyen.setName("jButton_DiChuyen"); // NOI18N
        jButton_DiChuyen.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton_DiChuyen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_DiChuyenActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton_DiChuyen);

        jSeparator3.setName("jSeparator3"); // NOI18N
        jToolBar1.add(jSeparator3);

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        jToolBar1.add(jLabel5);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jComboBox1);

        javax.swing.GroupLayout jPanel_DanhSachMailLayout = new javax.swing.GroupLayout(jPanel_DanhSachMail);
        jPanel_DanhSachMail.setLayout(jPanel_DanhSachMailLayout);
        jPanel_DanhSachMailLayout.setHorizontalGroup(
            jPanel_DanhSachMailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_DanhSachMailLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel_DanhSachMailLayout.setVerticalGroup(
            jPanel_DanhSachMailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel_DanhSachMailLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jSplitPane1.setTopComponent(jPanel_DanhSachMail);

        jPanel_ChiTietMail.setName("jPanel_ChiTietMail"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jEditorPane_chitietmail.setContentType(resourceMap.getString("jEditorPane_chitietmail.contentType")); // NOI18N
        jEditorPane_chitietmail.setName("jEditorPane_chitietmail"); // NOI18N
        jScrollPane2.setViewportView(jEditorPane_chitietmail);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel_chude.setBackground(resourceMap.getColor("jLabel_chude.background")); // NOI18N
        jLabel_chude.setFont(resourceMap.getFont("jLabel_chude.font")); // NOI18N
        jLabel_chude.setForeground(resourceMap.getColor("jLabel_chude.foreground")); // NOI18N
        jLabel_chude.setText(resourceMap.getString("jLabel_chude.text")); // NOI18N
        jLabel_chude.setName("jLabel_chude"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel_goitu.setFont(resourceMap.getFont("jLabel_goitu.font")); // NOI18N
        jLabel_goitu.setForeground(resourceMap.getColor("jLabel_goitu.foreground")); // NOI18N
        jLabel_goitu.setText(resourceMap.getString("jLabel_goitu.text")); // NOI18N
        jLabel_goitu.setName("jLabel_goitu"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel_ngay.setFont(resourceMap.getFont("jLabel_ngay.font")); // NOI18N
        jLabel_ngay.setForeground(resourceMap.getColor("jLabel_ngay.foreground")); // NOI18N
        jLabel_ngay.setText(resourceMap.getString("jLabel_ngay.text")); // NOI18N
        jLabel_ngay.setName("jLabel_ngay"); // NOI18N

        jLabel_den.setFont(resourceMap.getFont("jLabel_den.font")); // NOI18N
        jLabel_den.setForeground(resourceMap.getColor("jLabel_den.foreground")); // NOI18N
        jLabel_den.setText(resourceMap.getString("jLabel_den.text")); // NOI18N
        jLabel_den.setName("jLabel_den"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel_chude, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel_goitu)
                        .addGap(218, 218, 218)
                        .addComponent(jLabel3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_den)
                    .addComponent(jLabel_ngay))
                .addGap(171, 171, 171))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel_chude, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel_ngay))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel_goitu)
                    .addComponent(jLabel3)
                    .addComponent(jLabel_den)))
        );

        javax.swing.GroupLayout jPanel_ChiTietMailLayout = new javax.swing.GroupLayout(jPanel_ChiTietMail);
        jPanel_ChiTietMail.setLayout(jPanel_ChiTietMailLayout);
        jPanel_ChiTietMailLayout.setHorizontalGroup(
            jPanel_ChiTietMailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
        );
        jPanel_ChiTietMailLayout.setVerticalGroup(
            jPanel_ChiTietMailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_ChiTietMailLayout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE))
        );

        jSplitPane1.setRightComponent(jPanel_ChiTietMail);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 559, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 538, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
       
    }//GEN-LAST:event_formAncestorAdded

    private void jButton_XoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_XoaActionPerformed
        // TODO add your handling code here:
        ArrayList<Integer> cacIdDangChon = LayCacIdMailDuocChon();
        MyMailBUS mailBUS = new MyMailBUS();
        for (int i=0;i<cacIdDangChon.size();i++){
            try {
                mailBUS.xoaMail(cacIdDangChon.get(i).toString());
                refreshBang(mailBUS);
            } catch (IOException ex) {
                Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        refreshBang(mailBUS);

    }//GEN-LAST:event_jButton_XoaActionPerformed

    private void jButtonDDChuaDocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDDChuaDocActionPerformed
        // TODO add your handling code here:
         ArrayList<Integer> cacIdDangChon = LayCacIdMailDuocChon();
        MyMailBUS mailBUS = new MyMailBUS();
        for (int i=0;i<cacIdDangChon.size();i++){
            MyMailDTO mailDTO = new MyMailDTO();
            try {
                mailDTO = mailBUS.layMailVoidMailID(cacIdDangChon.get(i).toString());
                mailDTO.setM_TinhTrang(1);
                mailBUS.capNhatMail(mailDTO);
                refreshBang(mailBUS);
            } catch (IOException ex) {
                Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButtonDDChuaDocActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        //thach: viet code o day
        JComboBox cb = (JComboBox)evt.getSource();
        String strMucDo = (String)cb.getSelectedItem();
        int iMucDo = jClass_ChuDe.LaySTT(strMucDo);
        MyMailBUS mailBUS = new MyMailBUS();

        ArrayList<Integer> dsMail = LayCacIdMailDuocChon();
        for (int i = 0 ; i < dsMail.size();i++){
            try {
                MyMailDTO mailDTO = new MyMailDTO();
                mailDTO = mailBUS.layMailVoidMailID(dsMail.get(i).toString());
                mailDTO.setM_MucDo(iMucDo);
                mailBUS.capNhatMail(mailDTO);
                refreshBang(mailBUS);
            } catch (IOException ex) {
                Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton_DDDaDocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_DDDaDocActionPerformed
        // TODO add your handling code here:
          ArrayList<Integer> cacIdDangChon = LayCacIdMailDuocChon();
        MyMailBUS mailBUS = new MyMailBUS();
        for (int i=0;i<cacIdDangChon.size();i++){
            MyMailDTO mailDTO = new MyMailDTO();
            try {
                mailDTO = mailBUS.layMailVoidMailID(cacIdDangChon.get(i).toString());
                mailDTO.setM_TinhTrang(2);
                mailBUS.capNhatMail(mailDTO);
                refreshBang(mailBUS);
            } catch (IOException ex) {
                Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton_DDDaDocActionPerformed

    private void jButton_DiChuyenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_DiChuyenActionPerformed
        // TODO add your handling code here:
        MyMailBUS mailBUS = new MyMailBUS();
        ArrayList<Integer> dsMail = LayCacIdMailDuocChon();
        JDialog_ChonThuMucDich dialog_ChonThuMucDich = new JDialog_ChonThuMucDich(null, true);
        dialog_ChonThuMucDich.setVisible(true);
        String thuMucDich = dialog_ChonThuMucDich.getM_ThuMucDuocChon();

        dialog_ChonThuMucDich.dispose();
        if (thuMucDich.equals(""))
            return;
        int somailcapnhat = 0;
        for (int i = 0 ; i < dsMail.size();i++){
            try {
                mailBUS.thayDoiDuongDanThuMuc(dsMail.get(i).toString(), thuMucDich);
                somailcapnhat ++;
            } catch (IOException ex) {
                Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        JOptionPane.showMessageDialog(null, "Đã di chuyển " + somailcapnhat + " mail");
        refreshBang(mailBUS);
    }//GEN-LAST:event_jButton_DiChuyenActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonDDChuaDoc;
    private javax.swing.JButton jButton_DDDaDoc;
    private javax.swing.JButton jButton_DiChuyen;
    private javax.swing.JButton jButton_Xoa;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JEditorPane jEditorPane_chitietmail;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel_chude;
    private javax.swing.JLabel jLabel_den;
    private javax.swing.JLabel jLabel_goitu;
    private javax.swing.JLabel jLabel_ngay;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel_ChiTietMail;
    private javax.swing.JPanel jPanel_DanhSachMail;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    private JTable jTable_danhsachmail = new JTable();
    public void DuaDanhSachMailVaoBang(ArrayList<MyMailDTO>DanhSachMail) throws IOException, SQLException{
            final String cacCotDuLieu[] = {"Tất cả","Tình trạng","Đính kèm","Người Gởi","Chủ Đề", "Ngày", "Phân loại", "MailID"};
            Object DuLieu[][] = new Object[DanhSachMail.size()][cacCotDuLieu.length];
            for(int i = 0; i < DanhSachMail.size();i++){
                int index = 0;
                DuLieu[i][index++] = false;
               //for(int j = 1; i < model.) dulieu[index] = new Object();
                if (DanhSachMail.get(i).getM_TinhTrang() == JEnum_TinhTrang.Moi.value()){
                    Icon icon = new ImageIcon(this.getClass().getResource("/doanlythuyet_javaoutlook/MyUserControl/resources/new mail.png"));
                    DuLieu[i][index] = icon;
                }
                index++;
                if (new MyAttachBUS().layTatCaAttachCuaMailID(DanhSachMail.get(i).getM_Id()).size() != 0){
                    Icon icon = new ImageIcon(this.getClass().getResource("/doanlythuyet_javaoutlook/MyUserControl/resources/attach.png"));
                    DuLieu[i][index] = icon;
                }
                index++;
                DuLieu[i][index++] = DanhSachMail.get(i).getM_NguoiGoi();

                //dulieu[index++] = DanhSachMail.get(i).getM_NguoiGoi();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_ChuDe();
                DuLieu[i][index++] = DanhSachMail.get(i).getM_Ngay().toString();
                jClass_ChuDe chude = new jClass_ChuDe();
                int MucDo = DanhSachMail.get(i).getM_MucDo();
                if (MucDo > 0)
                    DuLieu[i][index++] = jClass_ChuDe.LayTen(MucDo);
                else
                    DuLieu[i][index++] = "";
                DuLieu[i][index++] = DanhSachMail.get(i).getM_Id();
                
            }
            final Object bangDuLieu[][] = DuLieu;
            DefaultTableModel modelBangHienThi = new DefaultTableModel(bangDuLieu,cacCotDuLieu) {

            @Override
            public Class getColumnClass(int columnIndex) {
                switch (columnIndex){
                    case 0: return Boolean.class;
                    case 1: return ImageIcon.class;
                    case 2: return ImageIcon.class;
                    default: return String.class;
                }
            }
                @Override
                public int getColumnCount() {
                    return cacCotDuLieu.length;
                }
                @Override
                public String getColumnName(int column) {
                    return cacCotDuLieu[column];
                }
                @Override
                public int getRowCount() {
                    return bangDuLieu.length;
                }
                @Override
                public Object getValueAt(int row, int column) {
                    return bangDuLieu[row][column];
                }
                @Override
                public void setValueAt(Object value, int row, int column) {
                    bangDuLieu[row][column] = value;
                }
                @Override
                public boolean  isCellEditable(int row, int column) {
                    return column == 0;
                }
            };

            jTable_danhsachmail.setModel(modelBangHienThi);
            
            //jTable_danhsachmail = new TableSelectionTest(cacCotDuLieu, bangDuLieu);
            TableColumn tc = jTable_danhsachmail.getColumnModel().getColumn(0);
            tc.setCellEditor(jTable_danhsachmail.getDefaultEditor(Boolean.class));
            tc.setCellRenderer(jTable_danhsachmail.getDefaultRenderer(Boolean.class));
            tc.setHeaderRenderer(new CheckBoxHeader(new MyItemListener()));

            // <editor-fold defaultstate="collapsed" desc="Cai dat giao dien cho bang">
            //jTable_danhsachmail.setSize(500, 100);
            jTable_danhsachmail.getColumnModel().getColumn(0).setMaxWidth(25);
            jTable_danhsachmail.getColumnModel().getColumn(jTable_danhsachmail.getColumnCount() - 1).setMinWidth(0);
            jTable_danhsachmail.getColumnModel().getColumn(jTable_danhsachmail.getColumnCount() - 1).setMaxWidth(0);
            jTable_danhsachmail.getColumnModel().getColumn(jTable_danhsachmail.getColumnCount() - 1).setPreferredWidth(0);
            //jTable_danhsachmail.
            // </ediort-fold>
            jScrollPane1.setViewportView(jTable_danhsachmail);
    }

    private void DoiTrangThaiCotCheckBox (boolean isDuocChon){
        TableModel tableModel = jTable_danhsachmail.getModel();
        for (int i = 0; i < tableModel.getRowCount(); i++){
            tableModel.setValueAt(isDuocChon, i, 0);
        }
    }

    /**
     * @return the popupMenu
     */
    public JPopupMenu getPopupMenu() {
        return popupMenu;
    }

    private void refreshBang(MyMailBUS mailBUS) {
        //thanh:refresh giao dien de? cap nhat
        String str_fileduocchon = getToolTipText();
        Hashtable<String, String> cacDieuKien = new Hashtable<String, String>();
        cacDieuKien.put("DuongDanThuMuc", str_fileduocchon);
        try {
            ArrayList<MyMailDTO> cacMailDTO = mailBUS.layTatCaMailVoiDieuKien(str_fileduocchon, cacDieuKien);
            DuaDanhSachMailVaoBang(cacMailDTO);
        } catch (SQLException ex) {
            Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JPanel_XemMail.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    class MyItemListener implements ItemListener{
     public void itemStateChanged(ItemEvent e) {
       Object source = e.getSource();
       if (source instanceof AbstractButton == false) return;
       boolean checked = e.getStateChange() == ItemEvent.SELECTED;
       DoiTrangThaiCotCheckBox(checked);
     }
   }
}
class CheckBoxHeader extends JCheckBox implements TableCellRenderer, MouseListener {
    protected CheckBoxHeader rendererComponent;
    protected int column;
    protected boolean mousePressed = false;
    public CheckBoxHeader(ItemListener itemListener) {
     rendererComponent = this;
     rendererComponent.addItemListener(itemListener);
    }
    public Component getTableCellRendererComponent(
        JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
        if (table != null) {
            JTableHeader header = table.getTableHeader();
            if (header != null) {
            rendererComponent.setForeground(header.getForeground());
            rendererComponent.setBackground(header.getBackground());
            rendererComponent.setFont(header.getFont());
            header.addMouseListener(rendererComponent);
            }
        }
         setColumn(column);
         rendererComponent.setText("");
         setBorder(UIManager.getBorder("TableHeader.cellBorder"));
         return rendererComponent;
    }
    protected void setColumn(int column) {
     this.column = column;
    }
    public int getColumn() {
     return column;
    }
    protected void handleClickEvent(MouseEvent e) {
        if (mousePressed) {
           mousePressed=false;
           JTableHeader header = (JTableHeader)(e.getSource());
           JTable tableView = header.getTable();
           TableColumnModel columnModel = tableView.getColumnModel();
           int viewColumn = columnModel.getColumnIndexAtX(e.getX());
           int columnindex = tableView.convertColumnIndexToModel(viewColumn);

           if (viewColumn == this.column && e.getClickCount() == 1 && columnindex != -1) {
             doClick();
           }
        }
    }
    public void mouseClicked(MouseEvent e) {
     handleClickEvent(e);
     ((JTableHeader)e.getSource()).repaint();
    }
    public void mousePressed(MouseEvent e) {
     mousePressed = true;
    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
}